/*
 * Created by 岱青海蓝信息系统(北京)有限公司 on 17-5-17 下午12:17
 * Copyright (c) 2017. All rights reserved.
 */

package com.example.dqhl.recycleviewdemo.view;

import android.content.Context;
import android.graphics.Rect;
import android.support.annotation.Nullable;
import android.support.v7.widget.CardView;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.AttributeSet;
import android.view.View;

/**
 * Created by dqhl on 17/5/17.
 */
public class ParallaxRecycleView extends RecyclerView {

    public ParallaxRecycleView(Context context) {
        super(context, null);
    }

    public ParallaxRecycleView(final Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        if (isInEditMode()) {
            return;
        }
        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(context);
        linearLayoutManager.setOrientation(LinearLayoutManager.HORIZONTAL);
        setLayoutManager(linearLayoutManager);

        addItemDecoration(new ItemDecoration() {
            @Override
            public void getItemOffsets(Rect outRect, View view, RecyclerView parent, State state) {
                super.getItemOffsets(outRect, view, parent, state);
                outRect.right = -dp2px(context,15);
            }
        });
        addOnScrollListener(new OnScrollListener() {
            @Override
            public void onScrollStateChanged(RecyclerView recyclerView, int newState) {
                super.onScrollStateChanged(recyclerView, newState);
            }

            @Override
            public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
                super.onScrolled(recyclerView, dx, dy);
                LinearLayoutManager layoutManager = (LinearLayoutManager) recyclerView.getLayoutManager();
                int firstPosition = layoutManager.findFirstVisibleItemPosition();
                int lastPosition = layoutManager.findLastVisibleItemPosition();
                int visibleCount = lastPosition - firstPosition;
                //重置控件的位置及高度
                int elevation = 1;
                for (int i = firstPosition - 1; i <= (firstPosition + visibleCount) + 1; i++) {
                    View view = layoutManager.findViewByPosition(i);
                    if (view != null) {
                        if (view instanceof CardView) {
                            ((CardView) view).setCardElevation(dp2px(context, elevation));
                            elevation += 5;
                        }
                        float translationX = view.getTranslationX();
                        if (i > firstPosition && translationX != 0) {
                            view.setTranslationX(5);
                        }
                    }
                }
                View firstView = layoutManager.findViewByPosition(firstPosition);
                float firstViewTop = firstView.getLeft();
                firstView.setTranslationX(-firstViewTop);
            }
        });
    }

    private int dp2px(Context context, float dipvalue) {
        final float scale = context.getResources().getDisplayMetrics().density;
        return (int) (dipvalue * scale + 0.5f);
    }
}
